from time import sleep
from selenium import webdriver
from selenium.webdriver.support.select import Select
driver = webdriver.Firefox()
# 打开登录页面
driver.get('http://localhost/hjq1999/index.asp')

# 完成登录
ipts = driver.find_elements_by_tag_name('input')
ipts[0].send_keys('admin')
ipts[1].send_keys('admin')
ipts[2].click()

sleep(2)
# 从当前默认位置切换到菜单框架中
driver.switch_to.frame('left')

# 点击菜单 系统设置
sysset_css = '.menuall > tbody:nth-child(1) > tr:nth-child(13) > td:nth-child(1)'
driver.find_element_by_css_selector(sysset_css).click()

sleep(2)
# 继续点击：计量单位管理
danwei_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(7) > td:nth-child(1)'
driver.find_element_by_css_selector(danwei_css).click()
sleep(2)

# 切换原始位置
driver.switch_to.default_content()
# 切到右侧框架
driver.switch_to.frame('right')
sleep(2)

# 点击全部选择按钮
driver.find_element_by_id('chkall').click()
sleep(2)

#点击删除按钮
bt_css = 'input.button:nth-child(2)'
driver.find_element_by_css_selector(bt_css).click()
sleep(2)

# 处理弹框
driver.switch_to.alert.accept()
sleep(2)

# 点击添加单位按钮
bt2_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(bt2_css).click()
sleep(2)


# 输入单位名称
driver.find_element_by_name('danwei').send_keys('千克')
# 点击确认按钮
driver.find_element_by_name('submit').click()
sleep(2)
# 处理弹框
driver.switch_to.alert.accept()
sleep(2)

#切换原始位置
driver.switch_to.default_content()

#切换到左侧
driver.switch_to.frame('left')

# 点击新增一个大类
big_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(5) > td:nth-child(1)'
driver.find_element_by_css_selector(big_css).click()
sleep(2)

# 切到原始位置
driver.switch_to.default_content()

# 切到右侧
driver.switch_to.frame('right')

#新增大类
biga_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(biga_css).click()
sleep(2)

#输入数据
driver.find_element_by_name('bigclass').send_keys('电脑配件')
sleep(2)

#确认
driver.find_element_by_name('submit').click()
sleep(2)
driver.switch_to.alert.accept()

# 切到原始位置
driver.switch_to.default_content()

#切换到左侧
driver.switch_to.frame('left')

# 新增一个小类
small_css = '#g_6 > table:nth-child(1) > tbody:nth-child(1) > tr:nth-child(6) > td:nth-child(1)'
driver.find_element_by_css_selector(small_css).click()
sleep(2)

# 切换原始位置
driver.switch_to.default_content()

# 切到右侧
driver.switch_to.frame('right')

#新增小类
smalla_css = 'body > table:nth-child(1) > tbody:nth-child(2) > tr:nth-child(1) > td:nth-child(2) > select:nth-child(1)'
driver.find_element_by_css_selector(smalla_css).click()
sleep(4)

#选择电子器械
sel = driver.find_element_by_name('bigclass')
lst = Select(sel)
lst.select_by_visible_text('电脑配件')
sleep(2)

#点击添加小类
smallb_css = 'input.button:nth-child(1)'
driver.find_element_by_css_selector(smallb_css).click()
sleep(2)

#填写小类
driver.find_element_by_name('smallclass') .send_keys("耳机")

#点击确认
driver.find_element_by_name('submit') .click()
sleep(2)
driver.switch_to.alert.accept()

# 切换原始位置
driver.switch_to.default_content()

# 切换到顶部框架
driver.switch_to.frame('topFrame')
driver.find_element_by_link_text('退出成功').click()
sleep(2)
driver.switch_to.alert.accept()
sleep(2)
# 关闭浏览器
driver.quit()
